package com.group11.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.group11.common.pojo.ResponseResult;
import com.group11.user.entity.Enterprise;
import com.group11.user.service.EnterpriseService;
import com.group11.user.service.UploadService;
import com.group11.user.vo.EnterpriseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private UploadService uploadService;

    @PostMapping("/add")
    public ResponseResult<Void> addEnterprise(@RequestHeader int id ,@RequestBody Enterprise enterprise) {
        if (enterpriseService.addEnterprise(enterprise, id)) {
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.FAILED;
    }

    @PostMapping("delete")
    public ResponseResult<Void> deleteEnterprise(String id) {
        UpdateWrapper<Enterprise> enterpriseUpdateWrapper = new UpdateWrapper<>();
        enterpriseUpdateWrapper.eq("id", id).set("is_deleted", "1");
        if (enterpriseService.update(enterpriseUpdateWrapper)) {
            return ResponseResult.SUCCESS;
        }
        return ResponseResult.FAILED;
    }

    @GetMapping("/get")
    public ResponseResult<?> getEnterpriseByUserId(@RequestHeader int id) {
        EnterpriseVo byId = enterpriseService.getByUserId(id);
        if (byId == null) {
            return ResponseResult.FAILED;
        }
        return new ResponseResult<>(byId);
    }

    @PostMapping("/list")
    public ResponseResult<List<EnterpriseVo>> getAllEnterprise() {
        return new ResponseResult<>(enterpriseService.getAllEnterprise());
    }

    @GetMapping("/getById")
    public ResponseResult<?> getEnterpriseById(@RequestParam int id) {
        EnterpriseVo byId = enterpriseService.getById(id);
        if (byId == null) {
            return ResponseResult.FAILED;
        }
        return new ResponseResult<>(byId);
    }

    @PostMapping("/{id}")
    public ResponseResult<EnterpriseVo> getEnterprise(@PathVariable int id) {
        Enterprise id1 = enterpriseService.getOne(new QueryWrapper<Enterprise>().eq("id", id));
        return new ResponseResult<>(new EnterpriseVo(id1));
    }


}
